from datetime import datetime


def load_layout():
    # Default layout configuration
    return {
        "total_seats": 50,
        "seat_prefix": ["A", "B", "C", "D", "E"],
        "seats_per_prefix_row": 10,
        "timeslots": ["08:00-12:00", "14:00-18:00", "19:00-22:00"]
    }


def get_available_seats(target_date, target_timeslot, all_reservations, layout_info):
    reserved_seats = set()

    for reservation in all_reservations:
        if reservation["date"] == target_date and reservation["timeslot"] == target_timeslot:
            reserved_seats.add(reservation["seat_id"])

    all_seats = generate_all_seats(layout_info)
    available_seats = [seat for seat in all_seats if seat not in reserved_seats]

    return available_seats, reserved_seats


def generate_all_seats(layout_info):
    seats = []
    for prefix in layout_info["seat_prefix"]:
        for num in range(1, layout_info["seats_per_prefix_row"] + 1):
            seats.append(f"{prefix}{num:02d}")
    return seats


def format_seat_status_display(target_date, target_timeslot, available_seats, reserved_seats, layout_info):
    output = []
    output.append(f"\n=== {target_date} {target_timeslot} 座位状态 ===")

    # Display available seats in rows of 10
    output.append("\n可用座位:")
    if available_seats:
        for i in range(0, len(available_seats), 10):
            row = available_seats[i:i+10]
            output.append(" ".join(row))
    else:
        output.append("无可用座位")

    # Display reserved seats in rows of 10
    output.append("\n已预约座位:")
    if reserved_seats:
        reserved_list = sorted(reserved_seats)
        for i in range(0, len(reserved_list), 10):
            row = reserved_list[i:i+10]
            output.append(" ".join(row))
    else:
        output.append("无预约座位")

    return "\n".join(output)


def view_seats_interaction(all_reservations, layout_info):
    print("\n=== 查看座位状态 ===")

    # Get date input
    while True:
        date_str = input("请输入日期 (YYYY-MM-DD): ")
        try:
            datetime.strptime(date_str, "%Y-%m-%d")
            break
        except ValueError:
            print("日期格式无效，请重新输入。")

    # Get timeslot input
    print("\n可选时段:")
    for i, timeslot in enumerate(layout_info["timeslots"], 1):
        print(f"{i}. {timeslot}")

    while True:
        try:
            choice = int(input("请选择时段 (1-3): "))
            if 1 <= choice <= len(layout_info["timeslots"]):
                timeslot = layout_info["timeslots"][choice - 1]
                break
            else:
                print("无效的选择，请重新输入。")
        except ValueError:
            print("请输入有效的数字。")

    # Get and display seat status
    available_seats, reserved_seats = get_available_seats(date_str, timeslot, all_reservations, layout_info)
    display_text = format_seat_status_display(date_str, timeslot, available_seats, reserved_seats, layout_info)
    print(display_text)